package com.ebelter.btcomlib.views;


import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ebelter.btcomlib.R;
import com.ebelter.btcomlib.utils.DipUtil;
import com.ebelter.btcomlib.utils.NumUtils;
import com.ebelter.btcomlib.utils.log.LogUtils;
import com.ebelter.btcomlib.views.dialogs.TextTipDialog;
import com.ebelter.btcomlib.views.dialogs.picktime2.DatePickerView;
import java.util.ArrayList;
import java.util.Calendar;


public class WeightSelectDialog {

    private String TAG = "WeightSelectDialog";
    private Context mContext;
    private LinearLayout date_root_ll;

    private Dialog pickerDialog;
    private DatePickerView year_pv, month_pv;
    private TextView tv_cancle, tv_select;

    private ArrayList<String> year, month;

    public void setBackgroundColor(int color) {
        if (date_root_ll!=null){
            date_root_ll.setBackgroundColor(color);
        }
    }

    /**
     * 定义结果回调接口
     */
    public interface ResultHandler {
        void handle(String time);
    }



    public WeightSelectDialog(Context mContext) {
        this.mContext = mContext;
        initDialog();
        initView();
        initArrayList();
        executeScroll();
        addListener();

    }

    private String myleft;
    private String myright;

    private void addListener() {
        year_pv.setOnSelectListener(new DatePickerView.onSelectListener() {
            @Override
            public void onSelect(String left) {
                LogUtils.i(TAG,"--year_pv--onSelect -- left = "+left);
                myleft=left;

                if (valueChange!=null){
                    float v=NumUtils.numBaoLiuWei1((float) (NumUtils.string2Int(myleft)+NumUtils.string2Int(myright)*1.0/10.0));
                    valueChange.change(v);
                }
            }
        });

        month_pv.setOnSelectListener(new DatePickerView.onSelectListener() {
            @Override
            public void onSelect(String right) {
                LogUtils.i(TAG,"--year_pv--onSelect -- right = "+right);
                myright=right;
                if (valueChange!=null){
                    float v=NumUtils.numBaoLiuWei1((float) (NumUtils.string2Int(myleft)+NumUtils.string2Int(myright)*1.0/10.0));
                    valueChange.change(v);
                }
            }
        });
    }


    private float selectF;


    private void initArrayList() {
        year = new ArrayList<>();
        month = new ArrayList<>();
        for (int i = 0;i<=180;i++){

            if (i>1){
                year.add(i+"");
            }

            if (i<10){
                month.add(i+"");
            }

        }
        year_pv.setData(year);
        month_pv.setData(month);
    }


    public void show(float selecNum) {
        this.selectF=selecNum;

        int leftNum=(int) selecNum;
        int rightNum = NumUtils.numBaoLiuWei0((NumUtils.numBaoLiuWei1(selecNum)-leftNum)*10);
        myleft=leftNum+"";
        myright=rightNum+"";
        year_pv.setSelected(leftNum-2);
        month_pv.setSelected(rightNum);
        pickerDialog.show();
    }


    private void initDialog() {
        if (pickerDialog == null) {
            pickerDialog = new Dialog(mContext, R.style.dialog_style);
            pickerDialog.setCancelable(true);
            pickerDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            pickerDialog.setContentView(R.layout.custom_weigh_picker);
            WindowManager.LayoutParams params = pickerDialog.getWindow().getAttributes();
            params.gravity = Gravity.BOTTOM;
            params.width = DipUtil.getWindowWidth(pickerDialog.getContext());
            pickerDialog.getWindow().setAttributes(params);
        }
    }


    private void initView() {
        year_pv = pickerDialog.findViewById(R.id.year_pv);
        month_pv =pickerDialog.findViewById(R.id.month_pv);
        date_root_ll =pickerDialog.findViewById(R.id.date_root_ll);

        tv_cancle = pickerDialog.findViewById(R.id.tv_cancle);
        tv_select = pickerDialog.findViewById(R.id.tv_select);

        tv_cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               if (pickerDialog!=null)pickerDialog.dismiss();
            }
        });

        tv_select.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                pickerDialog.dismiss();
                if (listener!=null){
                    listener.ok(pickerDialog);
                }
            }
        });
    }

    private TextTipDialog.ICancelOkBtListener listener;

    public void setListener(TextTipDialog.ICancelOkBtListener listener) {
        this.listener = listener;
    }

    private ValueChange valueChange;
    public interface ValueChange{
        void change(float value);
    }

    public void setValueChange(ValueChange valueChange) {
        this.valueChange = valueChange;
    }

    private void executeScroll() {
        year_pv.setCanScroll(year.size() > 1);
        month_pv.setCanScroll(month.size() > 1);
    }

    public void setOnDismissListener(DialogInterface.OnDismissListener listener){
        if (pickerDialog!=null){
            pickerDialog.setOnDismissListener(listener);
        }
    }

}
